package utstat.listeners;

import static utstat.Type.kill;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import utstat.Team;
import utstat.UtEvent;
import utstat.UtEventListener;

/**
 * xxx had an ill-conceived escape route Suicide within 3 seconds of receiving a hit
 * 
 * xxx's reserve chute failed to 2 lemming deaths in a game
 */
public class GenocideListener implements UtEventListener {

	private Map<String, Set<String>> kills = new HashMap<String, Set<String>>();

	private int currentRound = -1;

	@Override
	public String getIntestingFact(String player) {
		return null;
	}

	@Override
	public void processEvent(UtEvent e) {
		if (e.getRound() != currentRound) {
			nextRound();
			currentRound = e.getRound();
		}
		if (e.getType() == kill) {
			if(e.getSrc()==null) return;
			kills.put(e.getDest(), new HashSet<String>());

			Set<String> playersKilled = kills.get(e.getSrc());
			if (playersKilled == null) playersKilled = new HashSet<String>();
			playersKilled.add(e.getDest());
			kills.put(e.getSrc(), playersKilled);

			if (playersKilled.size() < 3) return;
			int enemyTeamSize = 0;
			for (Map.Entry<String, Team> entry : e.getPlayerTeams().entrySet())
				if (entry.getValue() == e.getEnemyTeam()) enemyTeamSize++;
			if (playersKilled.size() == enemyTeamSize) {
				System.out.println(e.getSrc() +" genocide!");
				kills.put(e.getSrc(), new HashSet<String>());
			}
		}

	}


	private void nextRound() {
		System.out.println("------");

		kills = new HashMap<String, Set<String>>();
	}

}
